package com.test.reactor.actor;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.socket.WebSocketSession;

import com.alibaba.fastjson.JSONObject;
import com.test.reactor.ws.MyMessage;

import akka.actor.AbstractActor;
import reactor.core.publisher.Flux;

@Component
@Scope(value = "prototype")
public class UserActor extends AbstractActor {
	private WebSocketSession session;
	
    public UserActor(WebSocketSession session) {
		super();
		this.session = session;
	}

	@Override
    public Receive createReceive() {
    	Receive build = receiveBuilder().match(MyMessage.class, msg -> {
            System.out.println(this.getSelf() + "--接受到msg：" + msg.toString());
            session.send(Flux.just(session.textMessage(JSONObject.toJSON(msg).toString()))).toProcessor();
            this.sender().tell("接受到消息", this.getSelf());
        }).matchAny(o -> {
            System.out.println(this.getSelf() + "--接受到：" + o);
            this.sender().tell("接受到消息", this.getSelf());
        }).build();
        return build;
    }
}
